﻿using UnityEngine;
using System.Collections;

public class Bone : MonoBehaviour {
    public GameObject stick
    {
        get;
        set;
    }

    public GameObject aa;
    public GameObject bb;
    public GameObject childStick;

    public Vector3 initA, initB;
    public void SetBone(GameObject ga, GameObject gb)
    {
        initA = ga.transform.localPosition;
        initB = gb.transform.localPosition;

        aa = ga;
        bb = gb;
        var go = GameObject.Instantiate<GameObject>(stick);
        go.transform.parent = this.gameObject.transform;
        childStick = go;
        ResetPos();
    }

    public void ResetPos()
    {
        var lr = childStick.GetComponent<LineRenderer>();
        lr.SetPosition(0, aa.transform.localPosition);
        lr.SetPosition(1, bb.transform.localPosition);
    }
    Quaternion qua;
    public void CalculateQua()
    {
        var ba = initB - initA;
        var dc = bb.transform.localPosition - aa.transform.localPosition;
        qua = Quaternion.FromToRotation(ba, dc);
    }

    public void CalculateVertex(Vector3 oldPos, GameObject ver)
    {
        var va = oldPos - initA;
        var vc = qua* va;
        var vNewPos = vc + aa.transform.localPosition;
        ver.transform.localPosition = vNewPos;
    }

}
